package cn.sirun.com.fca.car.status.view;

import android.app.Activity;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.tencent.cloud.iov.flow.view.IContentViewContainer;

import java.util.ArrayList;
import java.util.List;

import cn.sirun.com.fca.car.status.R;
import cn.sirun.com.fca.car.status.adapter.CarStatusAdapter;
import cn.sirun.com.fca.car.status.listener.MyItemLongClickListener;
import cn.sirun.com.fca.car.status.model.CarStatusData;
import cn.sirun.com.fca.car.status.presenter.ICarStatusPresenter;


public class CarStatusFlowView extends CarStatusBaseLoadingFlowView<ICarStatusPresenter> implements View.OnClickListener{

    private RecyclerView lvdata;//汽车参数
    private CarStatusAdapter adapter;
    private FrameLayout flgood;//正常对话框
    private LinearLayout llgood;//正常提示
    private FrameLayout flbad;//异常对话框
    private LinearLayout llbad;//异常提示
    private ImageView ivcarimg;//车图
    private Button flgoodbt;

    private FrameLayout flscancar; //扫描车况
    private ImageView sacimg;
    private String flag;//汽车状态 0 未启动 1 启动
    private FrameLayout flnocar;//汽车没有启动
    private LinearLayout scrollView;//汽车参数

    public CarStatusFlowView(Activity activity) {
        super(activity);
    }

    public CarStatusFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }
    @Override
    public void onCreate() {
        super.onCreate();

        View view = LayoutInflater.from(getContext())
                .inflate(R.layout.car_status_activity, null, false);
        setContentView(view);

        flscancar = view.findViewById(R.id.fl_scan_car_status);
        sacimg = view.findViewById(R.id.iv_scan_car_img);
        // 从左到右的平移动画
        Animation horizontalAnimation = new TranslateAnimation(2, 560, 2, 2);
        horizontalAnimation.setDuration(3000); // 动画持续时间
        horizontalAnimation.setRepeatCount(Animation.INFINITE); // 无限循环INFINITE
        //播放动画
        sacimg.setAnimation(horizontalAnimation);
        horizontalAnimation.start();
        new Handler().postDelayed(new Runnable(){
            public void run() {
                //execute the task
                flag="1";
                if(flag=="0"){
                    flscancar.setVisibility(View.GONE);
                    flnocar.setVisibility(View.VISIBLE);
                }else if(flag=="1"){
                    flscancar.setVisibility(View.GONE);
                    scrollView.setVisibility(View.VISIBLE);
                }

            }
        }, 6000);

        flnocar = view.findViewById(R.id.fl_scan_result_view);
        scrollView = view.findViewById(R.id.sl_car_status_datas);

        lvdata = view.findViewById(R.id.lv_car_status_data);
        flgood = view.findViewById(R.id.fl_car_status_good);
        llgood =  view.findViewById(R.id.ll_car_normal);
        flbad = view.findViewById(R.id.fl_car_status_bad);
        llbad =  view.findViewById(R.id.ll_car_abnormal);
        ivcarimg = view.findViewById(R.id.iv_car_condition);
        flbad.setOnClickListener(this);

        flgoodbt =view.findViewById(R.id.fl_car_status_good_bt);
        flgoodbt.setOnClickListener(this);

        lvdata.setLayoutManager(new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, false));
//        lvdata.setNestedScrollingEnabled(false);
        final List<CarStatusData> csdlist=new ArrayList<>();
//        for(int i=0;i<7;i++){
////            CarStatusData csd=new CarStatusData();
////            csd.setStatusName("汽车参数"+i);
////            csd.setStatusValue("数值"+i);
////            csdlist.add(csd);
////        }
        CarStatusData csd=new CarStatusData();
        csd.setStatusName("里程");
        csd.setStatusValue("15609KM");
        csdlist.add(csd);
        CarStatusData csd1=new CarStatusData();
        csd1.setStatusName("剩余油量");
        csd1.setStatusValue("80%");
        csdlist.add(csd1);
        CarStatusData csd2=new CarStatusData();
        csd2.setStatusName("故障指示灯状态");
        csd2.setStatusValue("正常");
        csdlist.add(csd2);
        CarStatusData csd3=new CarStatusData();
        csd3.setStatusName("充电系统状态");
        csd3.setStatusValue("异常");
        csdlist.add(csd3);
        CarStatusData csd4=new CarStatusData();
        csd4.setStatusName("电子节气门控制系统状态");
        csd4.setStatusValue("异常");
        csdlist.add(csd4);
        CarStatusData csd5=new CarStatusData();
        csd5.setStatusName("传输温度");
        csd5.setStatusValue("正常");
        csdlist.add(csd5);
        CarStatusData csd6=new CarStatusData();
        csd6.setStatusName("油压");
        csd6.setStatusValue("异常");
        csdlist.add(csd6);
        CarStatusData csd7=new CarStatusData();
        csd7.setStatusName("油箱盖");
        csd7.setStatusValue("正常");
        csdlist.add(csd7);
        CarStatusData csd8=new CarStatusData();
        csd8.setStatusName("更换机油");
        csd8.setStatusValue("正常");
        csdlist.add(csd8);
        CarStatusData csd9=new CarStatusData();
        csd9.setStatusName("油温过热警告");
        csd9.setStatusValue("异常");
        csdlist.add(csd9);
        CarStatusData csd10=new CarStatusData();
        csd10.setStatusName("防滑移制动系统状态");
        csd10.setStatusValue("异常");
        csdlist.add(csd10);
        CarStatusData csd11=new CarStatusData();
        csd11.setStatusName("制动防抱死系统状态");
        csd11.setStatusValue("异常");
        csdlist.add(csd11);
        adapter=new CarStatusAdapter(csdlist,getContext());
        adapter.setListener(new MyItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int postion) {
                CarStatusData csd = new CarStatusData();
                csd=adapter.getCarStatusData().get(postion);
                if(csd.getStatusValue().equals("异常")){
                    lvdata.setVisibility(View.GONE);
                    flgood.setVisibility(View.GONE);
                    flbad.setVisibility(View.VISIBLE);
                    llbad.setVisibility(View.VISIBLE);
                    ivcarimg.setImageResource(R.mipmap.car_bad);
                }else {
                        lvdata.setVisibility(View.GONE);
                        flgood.setVisibility(View.VISIBLE);
                        flbad.setVisibility(View.GONE);
                        llgood.setVisibility(View.VISIBLE);
                }
            }
        });
        lvdata.setAdapter(adapter);
    }


    @Override
    protected ICarStatusPresenter createPresenter() {
        return null;
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.fl_car_status_bad){
            lvdata.setVisibility(View.VISIBLE);
            flgood.setVisibility(View.GONE);
            flbad.setVisibility(View.GONE);
            llbad.setVisibility(View.GONE);
            ivcarimg.setImageResource(R.mipmap.car_good);
        }else if(view.getId()==R.id.fl_car_status_good_bt){
            lvdata.setVisibility(View.VISIBLE);
            flgood.setVisibility(View.GONE);
            flbad.setVisibility(View.GONE);
            llgood.setVisibility(View.GONE);
        }
    }
}
